﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public static class AccountDB
    {

        public static bool AuthenticateLogin(string userName, string password, out Account user)
        {
            user = null;
            var query = Model.GetContext()
                .AccountSets.FirstOrDefault(x => 
                    x.Email == userName  && x.Password == password);

            if(query != null)
            {
                user = query;
                return true;
            }
            return false;
        }

        public static bool AuthenticateRegisterValues(Account account)
        {
            //var query =
            //    (from a in Model.GetContext().AccountSets
            //    where a.Email == account.Email
            //    select a).FirstOrDefault();

            var query = Model.GetContext()
                .AccountSets.FirstOrDefault(a => 
                    a.Email == account.Email);

            if (query != null)
                return true;

            return false;          
        }

        public static void AddAccount(Account account)
        {            
            Model.GetContext().AccountSets.Add(account);
            Model.GetContext().SaveChanges();
        }

        public static List<Account> GetAccounts(AuthorityLevel authorityLevel)
        {
            return Model.GetContext().AccountSets.Where(a => 
                a.Authority == authorityLevel && !a.Deleted).ToList();
        }

        public static void DeleteAccount(Account account)
        {
            Model.GetContext().AccountSets.SingleOrDefault(a => 
                a.AccountId == account.AccountId).Deleted = true;
            
            Model.GetContext().SaveChanges();
        }

        public static void DeleteAccount(int id)
        {
            var acc = Model.GetContext().AccountSets.SingleOrDefault(x => x.AccountId == id);
            if (acc != null)
                DeleteAccount(acc);
        }
    }
}
